package com.example.bbang.mapper;

import com.example.bbang.entity.Money;

import com.example.bbang.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.relational.core.sql.In;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface MoneyMapper {

    @Select("select * from money where username=#{username}")
    @Transactional
    List<Money> findElseAll(String username);

    @Insert("INSERT INTO `money` ( `username`, `money`) VALUES (#{username},0);")
    @Transactional
    void save(Money money);






    @Update("UPDATE money set money = 0 where username=#{username}")
    void updateById(Money money);

//    update money set money=money+1 where username='yh'
    @Update ("<script> " +
            "update `money` " +
            "<set> " +
            "<if test = \"x != null and x!=''\">" +
            "money=money+#{x}," +
            "</if>" +
            "</set>" +
            " where username = #{username} " +
            "</script> ")
    void updateByUsername(@Param("username")String username,@Param("x")Integer x);

    @Update("<script> " +
            "update `money` " +
            "<set> " +
            "<if test = \"x != null and x!=''\">" +
            "money=money-#{x}," +
            "</if>" +
            "</set>" +
            " where username = #{username} " +
            "</script> ")
    void updateByUsername1(@Param("username")String username,@Param("x")Integer x);
}
